#include "singlesessiondetailinfo.h"
#include <QGridLayout>
#include <QPushButton>
#include "debug.h"
#include "choosefrienddialog.h"
#include "model/datacenter.h"


AvatarItem::AvatarItem(const QIcon& avatar,const QString& name)
{
    this->setFixedSize(70,80);
    QVBoxLayout* layout  = new QVBoxLayout(this);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignHCenter);
    layout->setContentsMargins(0,0,0,0);

    avatarBtn = new QPushButton;
    avatarBtn->setIcon(avatar);
    avatarBtn->setFixedSize(45,45);
    avatarBtn->setIconSize(QSize(45,45));
    avatarBtn->setStyleSheet("QPushButton{border:none;}");

    nameLabel = new QLabel;
    nameLabel->setText(name);
    QFont font("微软雅黑",12);
    nameLabel->setFont(font);
    nameLabel->setFixedHeight(24);
    nameLabel->setAlignment(Qt::AlignCenter);

    const int MAX_Width = 65;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    if(totalWidth>MAX_Width)
    {
        //省略右侧内容，保留MAX_Width宽度
        QString n =metrics.elidedText(name, Qt::ElideRight,MAX_Width);
        nameLabel->setText(n);
    }

    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel);

}
QPushButton* AvatarItem::getAvatar()
{
    return avatarBtn;
}



SingleSessionDetailInfo::SingleSessionDetailInfo(QWidget* parent,const model::UserInfo& userInfo)
    :QDialog(parent)
{
    //1.设置属性
    this->setWindowTitle("会话详情");
    this->setFixedSize(300,300);
    this->setAttribute(Qt::WA_DeleteOnClose);

    //2.设置布局管理器
    QGridLayout* layout = new QGridLayout(this);

    //3.向布局管理器添加控件

        //添加创建群聊Item
    AvatarItem* addItem = new AvatarItem(QIcon(":/myResource/Image/addMember.svg"),"添加");
    layout->addWidget(addItem,0,0);

        //添加单聊的另一个成员Item
#if TEST_UI    //测试数据
    AvatarItem* MemberItem = new AvatarItem(QIcon(":/myResource/Image/dogAvatar.svg"),"dog");
    layout->addWidget(MemberItem,0,1);
#endif
    AvatarItem* MemberItem = new AvatarItem(userInfo.avatar,userInfo.nickname);
    layout->addWidget(MemberItem,0,1);

        //添加删除好友按钮
    QPushButton* deleteBtn = new QPushButton;
    deleteBtn->setText("删除好友");
    deleteBtn->setFixedHeight(50);
    QString style ="QPushButton{border:1px solid black;border-radius:5px;background-color:white;}";
    style+="QPushButton:pressed{background-color:rgb(210,210,210);}";
    deleteBtn->setStyleSheet(style);
    layout->addWidget(deleteBtn,1,0,1,3);

    //4.添加信号和槽
    connect(addItem->getAvatar(),&QPushButton::clicked,this,[=]()
    {
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this,userInfo.userId);
        chooseFriendDialog->exec();
    });
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(deleteBtn,&QPushButton::clicked,this,[=]()
    {
        emit dataCenter->deleteFriend(userInfo.userId);
        this->close();
    });

}
